package com.xb.sourest.base;

import android.os.Bundle;

import androidx.annotation.Nullable;

/**
 * 这层主要是封装 懒加载+取消预加载
 */
public  abstract  class BaseChildLazyfragment extends BaseLazyFragment{


    /**
     * 是否初始化过布局
     */
    protected boolean isViewInitiated;
    /**
     * 当前界面是否可见
     */
    protected boolean isVisibleToUser;
    /**
     * 是否加载过数据
     */
    protected boolean isDataInitiated;


    protected  abstract  void  loadData();

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        isViewInitiated=true;
        prepareFetchData();

    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        this.isVisibleToUser = isVisibleToUser;
        if (isVisibleToUser) {
            prepareFetchData();
        }
    }


    /**
     * 判断懒加载条件
     */
    public void prepareFetchData() {
        if (isVisibleToUser && isViewInitiated && !isDataInitiated ) {
            loadData();
            isDataInitiated = true;
        }

    }

}
